<!DOCTYPE html>
<html DIR="LTR">
<head>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no"  /> <meta http-equiv="content-type" content="text/html; charset=UTF-8" />
<title>Google Maps JavaScript API v3</title>
<link href="http://code.google.com/apis/maps/documentation/javascript/examples/default.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>
<script type="text/javascript">
var initialLocation;
var browserSupportFlag =  new Boolean();
var map;
var infowindow = new google.maps.InfoWindow();
var marker = new google.maps.Marker();

function initialize() {
  var myOptions = {
    				zoom: 17,
    				mapTypeId: google.maps.MapTypeId.ROADMAP
 				  };
  map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
  
  // Try W3C Geolocation method (Preferred)
  if(navigator.geolocation) 
  {
    browserSupportFlag = true;
    navigator.geolocation.getCurrentPosition(function(position) {
      initialLocation = new google.maps.LatLng(position.coords.latitude,position.coords.longitude);
      //contentString = "Location found using W3C standard";
      map.setCenter(initialLocation);
      //infowindow.setContent(contentString);
      //infowindow.setPosition(initialLocation);
      //infowindow.open(map);
    }, function() {
      handleNoGeolocation(browserSupportFlag);
    });
  } 
  else if (google.gears) 
  {
    // Try Google Gears Geolocation
    browserSupportFlag = true;
    var geo = google.gears.factory.create('beta.geolocation');
    geo.getCurrentPosition(function(position) {
      initialLocation = new google.maps.LatLng(position.latitude,position.longitude);
      //contentString = "Location found using Google Gears";
      map.setCenter(initialLocation);
      //infowindow.setContent(contentString);
      //infowindow.setPosition(initialLocation);
     // infowindow.open(map);
    }, function() {
      handleNoGeolocation(browserSupportFlag);
    });
  } 
  else 
  {
    // Browser doesn't support Geolocation
    browserSupportFlag = false;
    handleNoGeolocation(browserSupportFlag);
  }
  google.maps.event.addListener(map, 'click', 
				function(event) {placeMarker(event.latLng);});

}

function handleNoGeolocation(errorFlag) {
  if (errorFlag == true) {
    initialLocation = newyork;
    contentString = "Error: The Geolocation service failed.";
  } else {
    initialLocation = siberia;
    contentString = "Error: Your browser doesn't support geolocation. Are you in Siberia?";
  }
  map.setCenter(initialLocation);
  infowindow.setContent(contentString);
  infowindow.setPosition(initialLocation);
  infowindow.open(map);
}


  
function placeMarker(location) {
    var clickedLocation = new google.maps.LatLng(location);
    marker.setMap(null);
	marker = new google.maps.Marker({
	position: location, 
	map: map
	});
	var new_lat = location.lat();
	var new_longi = location.lng();
	  google.maps.event.addListener(marker, 'click', 
			function() {window.location.href = "hello_controller/choose_place?lat="+new_lat+"&longi="+new_longi;} );
  //map.setCenter(location);
   
      map.setCenter(initialLocation);
      
   contentString = "Click the Marker to Continue";
   infowindow.setContent(contentString);
   infowindow.setPosition(new google.maps.LatLng(location.lat(),location.lng()) );
   infowindow.open(map);
}
</script>
</head>
<body onload="initialize()">
  <div id="map_canvas"></div>
</body>
</html>
